package com.pdsu.graduation_design.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.adapter.MainGridViewAdapter;
import com.pdsu.graduation_design.adapter.MainMenuBarAdapter;
import com.pdsu.graduation_design.domain.CommodityType;
import com.pdsu.graduation_design.domain.Type;
import com.pdsu.graduation_design.listener.MainGridViewOnItemClickListener;
import com.pdsu.graduation_design.listener.MainMenuBarOnItemClickListener;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class Main_Fragment extends Fragment  {

    private boolean isF=true;
    private EditText main_fragment_search_et;
    private TextView main_fragment_search_tvb;
    private View currentView;
    private TextView bar_top_textview;
    private List<String> taps2;

    private VerticalTabLayout verticalTabLayout;
    private GridView main_gridview;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        currentView=inflater.inflate(R.layout.fragment_main,null);
        init();
        operation();
        search();
        return currentView;

    }
    private void operation() {
        bar_top_textview.setText("助农");
        //获取一级分类
        RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodityType/getTypeList");
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                try {
                    Result r= JsonUtil.JsonToObj(result,Result.class);

                    if (r.getCode().equals("200")){

                        List<Type> types= JsonUtil.json2list(r.getData().toString(),Type.class);
                        bind(types);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });
    }


    void bind(List<Type> types){
        /**
         * 侧边导航栏绑定数据
         */
        verticalTabLayout.setTabAdapter(new MainMenuBarAdapter(types));
//        verticalTabLayout.addOnTabSelectedListener(new MainMenuBarOnItemClickListener(currentView.getContext()));
        verticalTabLayout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodityType/getCommodityListByTID");
                requestParams.addParameter("tid",types.get(position).getTid());
                x.http().post(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {

                        try {
                            Result r= JsonUtil.JsonToObj(result,Result.class);

                            if (r.getCode().equals("200")){

                                List<CommodityType> commodityTypes= JsonUtil.json2list(r.getData().toString(),CommodityType.class);
                                main_gridview.setAdapter(new MainGridViewAdapter(commodityTypes,currentView.getContext()));
                                main_gridview.setOnItemClickListener(new MainGridViewOnItemClickListener(currentView.getContext(),commodityTypes));
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }
                    @Override
                    public void onCancelled(CancelledException cex) {

                    }
                    @Override
                    public void onFinished() {

                    }
                });
            }
            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });


        /**
         * 右侧数据列表
         */
        if (isF){
            RequestParams requestParams=new RequestParams(MainActivity.URL+"/commodityType/getCommodityListByTID");
            requestParams.addParameter("tid",types.get(0).getTid());
            x.http().post(requestParams, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {

                    try {
                        Result r= JsonUtil.JsonToObj(result,Result.class);

                        if (r.getCode().equals("200")){

                            List<CommodityType> commodityTypes= JsonUtil.json2list(r.getData().toString(),CommodityType.class);
                            main_gridview.setAdapter(new MainGridViewAdapter(commodityTypes,currentView.getContext()));
                            main_gridview.setOnItemClickListener(new MainGridViewOnItemClickListener(currentView.getContext(),commodityTypes));
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }


                }
                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }
                @Override
                public void onCancelled(CancelledException cex) {

                }
                @Override
                public void onFinished() {

                }
            });

            isF=true;
        }



    }
    private void init() {
        main_fragment_search_et = currentView.findViewById(R.id.main_fragment_search_et);
        main_fragment_search_tvb=currentView.findViewById(R.id.main_fragment_search_tvb);
        bar_top_textview = currentView.findViewById(R.id.bar_top_common_textview);
        verticalTabLayout = currentView.findViewById(R.id.main_bar);
        main_gridview=currentView.findViewById(R.id.main_gridview);
        //taps
        taps2=new ArrayList<>();

    }


    void search(){

        main_fragment_search_tvb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = main_fragment_search_et.getText().toString();
                if (data.length()==0){
                    Toast.makeText(getContext(), "请输入内容", Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(getContext(),Main_Search_Activity.class);
                    intent.putExtra("data",data);
                    startActivity(intent);
                }

            }
        });



    }



}
